package com.luli.controllor;

import com.luli.entity.photo.Paths;
import com.luli.entity.photo.Photo;
import com.luli.entity.response.ResponseMsg;
import com.luli.service.PhotoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

import static com.luli.Utils.PojoUtil.uploadFile;
import static com.luli.Utils.ResponseMsgUtil.getFailMsg;
import static com.luli.Utils.ResponseMsgUtil.getSuccessMsg;

/**
 * 陆离
 * 2022/5/7 9:50:19
 */
@RestController
@Slf4j
@RequestMapping("/photo")
@CrossOrigin
public class PhotoController {

    @Autowired
    private PhotoService photoService;

    @Autowired
    private Paths paths;

    @RequestMapping(value = "/addPhoto", method = RequestMethod.GET)
    public ResponseMsg<?> addPhoto(Photo photoTest) {
        try {
            Integer integer = photoService.addPhoto(photoTest);
            return getSuccessMsg(null, "成功新增：" + integer + "条数据", null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return getFailMsg(null, "新增失败", e.getMessage());
        }
    }

    @RequestMapping("/addFileUpload")
    public ResponseMsg<?> addFileUpload( @RequestParam MultipartFile upload){
        try {
            HashMap<String, Object> uploadPhotoMap = uploadFile(upload, paths.getReportUploadPath());
            if ("true".equals(uploadPhotoMap.get("static"))) {
                return getSuccessMsg(null,"上传图片成功",null);
            }else {
                return getFailMsg(null,"上传图片失败",null);
            }
        }catch (Exception e){
            e.getMessage();
            return getFailMsg(null,"上传图片失败",null);
        }
    }


    @RequestMapping(value = "/delPhoto",method = RequestMethod.POST)
    public ResponseMsg<?> delPhoto(  @RequestParam("photoPath")String photoPath, @RequestParam("photoId") String photoId){
        try {
            Integer integer = photoService.deletePhoto(photoPath, photoId);
            if (integer==1) {
                return getSuccessMsg(null,"图片删除成功",null);
            }
            return getFailMsg(null,"图片删除失败",null);

        }catch (Exception e){
            e.getMessage();
            return getFailMsg(null,"图片删除失败",null);
        }
    }





}
